#include "httplib.h"

void Helloworld(const httplib::Request& req, httplib::Response& rsp)
{
  rsp.body = "hello world";
  rsp.status = 200;
  return ;
}
void Numbers(const httplib::Request& req, httplib::Response& rsp)
{
  // /numbers/(\d+) 这个正则表达式匹配的是 /numbers/字符串后面跟了一串数字字符，并捕获这串数字字符
  // /numbers/1234
  // std::cout << req.matches[0] << std::endl; //保存的是URL ->/numbers/1234
  // std::cout << req.matches[1] << std::endl; //保存的是 /numbers/ 后面的数字 ->1234
  std::string num = req.matches[1];
  rsp.body = num;
  rsp.status = 200;
}
void Dish(const httplib::Request& req, httplib::Response& rsp)
{
	rsp.body = req.body;
	rsp.status = 200;
}

int main()
{
  httplib::Server srv;

  //当客户端请求的是一个静态的实体文件资源的时候，就会直接读取文件进行响应
	srv.set_mount_point("/","./Default_Path");
  //设置静态资源根目录 /index.html -> ./Default_Path/index.html

  srv.Get("/hi",Helloworld);// GET /hi HTTP/1.1
  //正则表达式:字符串的匹配机制, \d+ 表示匹配一个或多个连续的数字字符
  srv.Get(R"(/numbers/(\d+))",Numbers); // GET /numbers/123 HTTP/1.1

  srv.Post("/dish",Dish);

  //搭建Tcp服务器，开始监听，获取新连接，接受http请求数据，进行解析，调用对应的处理函数，组织响应
  srv.listen("0.0.0.0",4080);
  return 0;
}
